package com.atguigu.gulimall.product.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //test1();
        //test2();
        //test3();
        //test4();
        //test5();
        test6();
    }

    public static void test1() throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).whenCompleteAsync((r,t)->{
            System.out.println("结果是:"+r+"--异常是:"+t);
            System.out.println("线程id是:"+Thread.currentThread().getId());
        }).exceptionally(t-> {
            System.out.println(t);
            return 10;
        });
        Integer result = future.get();
        System.out.println(result);
        System.out.println("main......end");
    }

    public static void test2() throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 3;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).handle((r,t)->{
            if (r!=null)
                return r*2;
            if (r!=null)
                return 0;
            return 0;
        });
        Integer result = future.get();
        System.out.println(result);
        System.out.println("main......end");
    }

    public static void test3() throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 3;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenRunAsync(()->{
            System.out.println("线程2:"+Thread.currentThread().getId());
        },executor);
        System.out.println("main......end");
    }

    public static void test4() throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 3;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).thenAcceptAsync(r->{
            System.out.println("线程2:"+Thread.currentThread().getId());
            System.out.println("上个任务的结果为:"+r);
        },executor);
        System.out.println("main......end");
    }

    public static void test5() throws ExecutionException, InterruptedException {
        System.out.println("main start");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 1;
        }, executor);
        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            System.out.println("任务2结束");
            return 2;
        }, executor);
        //future01.runAfterBothAsync(future02,()-> System.out.println("任务3结束"),executor);
        future01.thenAcceptBothAsync(future02,(r1,r2)->{
            System.out.println("任务3开始,任务1和任务2的结果:"+r1+"--"+r2);
        },executor);

        System.out.println("main......end");
    }

    public static void test6() throws ExecutionException, InterruptedException {
        System.out.println("main......start");
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程:" + Thread.currentThread().getId());
            System.out.println("任务1结束");
            return 3;
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程:" + Thread.currentThread().getId());
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束");
            return "Hello";
        }, executor);
        future01.runAfterEitherAsync(future02,()->{
            System.out.println("任务3开始");
        },executor);
        System.out.println("main......end");
    }
}
